package com.kun.utils;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.util.ArrayList;

public class XmlUtils {

    /**
     * 获取其子节点转换成List集合，并过滤出节点
     */
    public static ArrayList<Node> findChildrenByName(NodeList nodeList, String name) {
        ArrayList<Node> nodes = new ArrayList<>();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node item = nodeList.item(i);
            if (item.getNodeType() == Node.ELEMENT_NODE && item.getNodeName().equals(name)) {
                nodes.add(item);
            }
        }
        return nodes;
    }

    /**
     * 不进行过滤，将节点转化为List集合
     */
    public static ArrayList<Node> findChildren(NodeList nodeList) {
        ArrayList<Node> nodes = new ArrayList<>();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node item = nodeList.item(i);
            if (item.getNodeType() == Node.ELEMENT_NODE) {
                nodes.add(item);
            }
        }
        return nodes;
    }

}
